<%@ page import="bean.DB" %>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户中心</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: flex-start;
            height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }

        .container {
            display: flex;
            width: 100%;
            max-width: 1200px;
            padding: 20px;
        }

        .sidebar, .content {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .sidebar {
            width: 300px; /* 增加宽度以便更好显示用户信息 */
            margin-right: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            position: relative;
        }

        .content {
            flex: 1; /* 使商品区域占据剩余空间 */
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
            max-height: 600px;
            overflow-y: auto;
        }

        /* 样式保持不变，其他部分代码不变 */
        .user-points-label {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: #4CAF50;
            color: white;
            padding: 5px 10px;
            border-radius: 5px;
            font-size: 14px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }

        .avatar {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: #ddd;
            margin-bottom: 20px;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
        }

        .avatar img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .username {
            font-size: 18px;
            margin-bottom: 10px;
            font-weight: bold;
        }

        .info-list {
            list-style: none;
            padding: 0;
            width: 100%;
        }

        .info-list li, .logout-btn {
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            margin-bottom: 12px;
            border-radius: 4px;
            text-align: center;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
            border: none;
        }

        .logout-btn {
            margin-bottom: 0;
            width: 100%;
        }

        .info-list li:hover, .logout-btn:hover {
            background-color: #45a049;
        }

        .logout-btn:hover {
            background-color: red;
        }

        .product {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            width: 100%;
            padding: 15px;
            text-align: center;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease-in-out;
            background-color: #fff;
        }

        .product:hover {
            transform: scale(1.05);
        }

        .product img {
            width: 100%;
            height: 200px;
            object-fit: cover;
            border-radius: 4px;
            margin-bottom: 10px;
        }

        .product h3 {
            font-size: 16px;
            margin: 8px 0;
            font-weight: bold;
            color: #333;
        }

        .product p {
            font-size: 14px;
            margin: 4px 0;
            color: #666;
        }

        .product-footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 10px;
        }

        .quantity {
            width: 40px;
            padding: 5px;
            border-radius: 5px;
            border: 1px solid #ccc;
            font-size: 14px;
        }

        .buy-btn {
            padding: 5px 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }

        .buy-btn:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="sidebar">
        <%
            DB db = new DB();
            int userPoints = 0;
            String userId = (String) session.getAttribute("userId");
            if (userId != null) {
                String pointsQuery = "SELECT user_star FROM users WHERE id = ?";
                PreparedStatement ps = db.createConn().prepareStatement(pointsQuery);
                ps.setString(1, userId);
                ResultSet pointsRs = ps.executeQuery();
                if (pointsRs.next()) {
                    userPoints = pointsRs.getInt("user_star");
                }
                pointsRs.close();
                ps.close();
            }
        %>
        <label class="user-points-label">
            积分: <%= userPoints %>
        </label>

        <div class="avatar">
            <a href="#" onclick="showUploadModal()">
                <%
                    // 获取用户头像路径，若未设置头像则使用默认头像
                    String avatarPath = (String) session.getAttribute("user_avatar");
                    if (avatarPath == null || avatarPath.isEmpty()) {
                        avatarPath = "image/public/defaultAvatar.jpg"; // 默认头像路径
                    }
                %>
                <img src="<%= avatarPath %>" alt="头像" onerror="this.onerror=null;this.src='image/public/defaultAvatar.jpg';">
            </a>
        </div>
        <div class="username">
            <%= session.getAttribute("username") != null ? session.getAttribute("username") : "未设置用户名" %>
        </div>
        <ul class="info-list">
            <li onclick="window.location.href='userPage.jsp'">个人信息</li>
            <li onclick="window.location.href='myOrders.jsp'">我的订单</li>
            <li onclick="window.location.href='cart.jsp'">我的购物车</li>
            <li onclick="window.location.href='managerReg.jsp'">加入我们</li>
            <form action="./logoutServlet">
                <button class="logout-btn">退出登录</button>
            </form>
            <li onclick="window.location.href='cancellation_account.jsp'">注销账户</li>
        </ul>
    </div>

    <div class="content">
        <%
            String query = "SELECT id, product_name, price, description, image_path, star FROM products";
            ResultSet rs = db.createConn().prepareStatement(query).executeQuery();
        %>

        <div id="productsContainer">
            <%
                if (rs != null) {
                    while (rs.next()) {
                        int id = rs.getInt("id");
                        String productName = rs.getString("product_name");
                        double price = rs.getDouble("price");
                        double star = rs.getDouble("star");
                        String description = rs.getString("description");
                        String imagePath = rs.getString("image_path");
            %>
            <form action="./addToCartServlet" method="post">
                <div class="product" id="product-<%= id %>">
                    <img src="<%= imagePath %>" alt="<%= productName %>">
                    <h3><%= productName %></h3>
                    <p>价格: ¥<%= price %></p>
                    <p>积分: ¥<%= star %></p>
                    <p><%= description %></p>
                    <div class="product-footer">
                        <input type="hidden" name="productId" value="<%= id %>">
                        <input type="number" name="quantity" value="1" class="quantity" min="1" max="100">
                        <button type="submit" class="buy-btn">添加到购物车</button>
                    </div>
                </div>
            </form>
            <%
                }
            } else {
            %>
            <p>没有找到商品</p>
            <%
                }
            %>
        </div>
    </div>
</div>

<!-- 上传头像模态框 -->
<div id="uploadModal" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000;">
    <h3>上传新头像</h3>
    <form action="./updateProfileServlet" method="post" enctype="multipart/form-data">
        <input type="file" name="newAvatar" accept="image/*" required>
        <br><br>
        <button type="submit" style="padding: 10px 20px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer;">上传</button>
        <button type="button" onclick="closeUploadModal()" style="padding: 10px 20px; background-color: #ccc; border: none; border-radius: 5px; cursor: pointer; margin-left: 10px;">取消</button>
    </form>
</div>

<!-- 遮罩层 -->
<div id="modalOverlay" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 999;" onclick="closeUploadModal()"></div>

<script>
    function showUploadModal() {
        document.getElementById('uploadModal').style.display = 'block';
        document.getElementById('modalOverlay').style.display = 'block';
    }

    function closeUploadModal() {
        document.getElementById('uploadModal').style.display = 'none';
        document.getElementById('modalOverlay').style.display = 'none';
    }
</script>
</body>
</html>
